package com.weishi.common.utils;

import java.util.List;

/**
 * 数据简单分析工具
 */
public class DataAnalysisUtils {

    /**
     * 获取波峰的个数
     * @param dataList
     * @return
     */
    public static int getPeakNum(List<Double> dataList){
        double[] data = new double[dataList.size()];
        int index = 0;
        for(Double aa : dataList){
            data[index++] = aa;
        }
        int peak=0;

        try {
            //需要三个不同的值进行比较，取lo,mid，hi分别为三值
            for (int lo=0,mid=1,hi=2;hi<data.length;hi++){
                //先令data[lo]不等于data[mid]
                while (mid<data.length&&data[mid]==data[lo]){
                    mid++;
                }

                hi=mid+1;

                //令data[hi]不等于data[mid]
                while (hi<data.length&&data[hi]==data[mid]){
                    hi++;
                }

                if (hi>=data.length){
                    break;
                }

                //检测是否为峰值
                if (data[mid]>data[lo]&&data[mid]>data[hi]){
                    peak++;
                }

                lo=mid;
                mid=hi;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return peak;
    }


    /**
     * 获取波峰的个数(两个波峰之间必然有一个波谷，而两个波谷之间也必然有一个波峰)
     * @param dataList
     * @return
     */
    public static int getPeakNumPlus(List<Double> dataList){
        double[] data = new double[dataList.size()];
        int index = 0;
        for(Double aa : dataList){
            data[index++] = aa;
        }
        int peak=0;

        try {
            double[] PeakAndTrough=new double[data.length];

            //需要三个不同的值进行比较，取lo,mid，hi分别为三值
            for (int lo=0,mid=1,hi=2;hi<data.length;hi++){
                //先令data[lo]不等于data[mid]
                while (mid<data.length&&data[mid]==data[lo]){
                    mid++;
                }

                hi=mid+1;

                //令data[hi]不等于data[mid]
                while (hi<data.length&&data[hi]==data[mid]){
                    hi++;
                }

                if (hi>=data.length){
                    break;
                }

                //检测是否为峰值
                if (data[mid]>data[lo]&&data[mid]>data[hi]){
                    PeakAndTrough[mid]=1;       //1代表波峰
                }else if(data[mid]<data[lo]&&data[mid]<data[hi]){
                    PeakAndTrough[mid]=-1;      //-1代表波谷
                }

                lo=mid;
                mid=hi;
            }

            //计算均值
            float ave=0;
            for (int i=0;i<data.length;i++){
                ave+=data[i];
            }
            ave/=data.length;

            //排除大于均值的波谷和小于均值的波峰
            for (int i=0;i<PeakAndTrough.length;i++){
                if ((PeakAndTrough[i]>0&&data[i]<ave)||(PeakAndTrough[i]<0&&data[i]>ave)){
                    PeakAndTrough[i]=0;
                }
            }

            //统计波峰数量
            /*for (int i=0;i<PeakAndTrough.length;){
                while (i<PeakAndTrough.length&&PeakAndTrough[i]<=0){
                    i++;
                }

                if (i>=PeakAndTrough.length){
                    break;
                }

                peak++;

                while (i<PeakAndTrough.length&&PeakAndTrough[i]>=0){
                    i++;
                }
            }*/

            //统计波谷数量
            for (int i=0;i<PeakAndTrough.length;i++){
                if(PeakAndTrough[i]==-1){
                    peak++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return peak;
    }

}
